package handler

import (
	"context"
	"fmt"
	"hertz/demo/biz/boot"
	"hertz/demo/biz/util"
	"net/http"

	"github.com/cloudwego/hertz/pkg/app"
)

func GetInstanceLabel(ctx context.Context, c *app.RequestContext) {
	schema := boot.GetInstanceSchema()
	c.JSON(http.StatusOK, schema)
}

func GetAllViews(ctx context.Context, c *app.RequestContext) {

	type View struct {
		Label string `json:"label"`
		Value string `json:"value"`
	}
	ret := []View{}

	schema := boot.GetViewSchema()
	for _, v := range schema {
		ret = append(ret, View{
			Label: v.Label,
			Value: v.Name,
		})
	}

	c.JSON(http.StatusOK, ret)
}

func GetView(ctx context.Context, c *app.RequestContext) {
	label := c.Param("view")
	if label == "" {
		util.BadRequest(c, fmt.Errorf("missing view param"))
		return
	}

	schema := boot.GetViewSchema()
	v, found := schema[label]
	if !found {
		util.NotFound(c)
		return
	}

	c.JSON(http.StatusOK, v)
}
